#!/usr/bin/env python
# -*- coding: utf-8 -*-
import cexapi
import datetime
import time
import re

credentialFile = open('credentials.txt', 'r+')

for line in credentialFile:
        resultLine = re.split(';', line)  
        if resultLine[0]=='hannesh':
            apiUser=resultLine[0]
            apiCred1=resultLine[1]
            apiCred2=resultLine[2]      

demo = cexapi.api(apiUser, apiCred1, apiCred2)

current_orders = demo.current_orders()

sortedCurrOrders = sorted(current_orders, key=lambda order: order['price'], reverse=True) 

firstOrder=sortedCurrOrders[0]

for order in sortedCurrOrders:
    print order['type'],"price=",order['price'], "amount=",order['amount'], "pending=",order['pending'], "id=",order['id']
    lastOrderId=order['id']
    lastOrder=order

blockSize=100

firstOrders = sortedCurrOrders[0:blockSize]

print "---"

for order in firstOrders:
    print order['type'],"price=",order['price'], "amount=",order['amount'], "pending=",order['pending'], "id=",order['id']
    demo.cancel_order(order['id'])
    
print "xxxxxxxxxxxxxxx"

lastOrders = sortedCurrOrders[-blockSize:-1]

for order in lastOrders:
    print order['type'],"price=",order['price'], "amount=",order['amount'], "pending=",order['pending'], "id=",order['id']
    #demo.cancel_order(order['id'])
  

#print "firstOrder=",firstOrder
#print "lastOrder=",lastOrder
 
#demo.cancel_order(firstOrder['id'])    
#demo.cancel_order(lastOrder['id'])
